
# info.pm
#
# information generation and display commands.

package Purl::Commands::info;
use strict;
use Purl::Config;
use Purl::Template;
use constant NONE => -1;

sub import {

    Purl::Commands::register(
        name                  => "WHO",
        subRef                => \&Purl::Commands::info::who,
        shortCommandException => 1,
        aliases               => "DOING"
    );
}

sub who {
    my %args = @_;
    my $e;
    my $hash;
    my $i;
    my $idlesecs;
    my $idle;
    my $onsecs;
    my $on;
    my $whostr;
    my %tData;
    
        $tData{template} = 'basicLoop';
   

    if (   ( $args{command} ne "DOING" )
        && ( &Purl::tests::isWizard( $args{actor} ) ) )
    {
        $tData{header} = sprintf(
            "%-14.14s%-12.12s%-9.9s%-7.7s%-38s",
            "Player Name  ",
            "   On For", "   Idle", "Room", "Site"
        );

        my @activeClients = Purl::Network::getActiveClients();
        my @lines;
        for ( $i = $#activeClients; $i >= 0; $i-- ) {
            $e = $activeClients[$i]->{id};
            if ( $e != NONE ) {
                $idlesecs = $main::now - $main::objects[$e]{"last"};
                $idle     = &Purl::filter::timeFormat($idlesecs);
                $onsecs   = $main::now - $main::objects[$e]{"on"};
                $on       = &Purl::filter::timeFormat($onsecs);
                my $name = sprintf( "%-14.14s", $main::objects[$e]{"name"} );
                my $loc  = "#" . $main::objects[$e]{"location"};
                my $site =
                    ( $main::objects[$e]{"site"} eq "" )
                    ? $main::objects[$e]{"ip"}
                    : $main::objects[$e]{"site"};
                push @lines,
                    sprintf( "%-4.14s%9.9s%10.10s%9.9s%-38s",
                    $name, $on, $idle, "$loc   ", $site );
            }
        }
        $tData{lines} = \@lines;
    }
    else {
        $tData{header} = sprintf( "%-19.19s%-7.7s%-6.6s%-46s",
            "Player Name", "On For", " Idle ", "Doing" );
        my @lines;

        my @activeClients = Purl::Network::getActiveClients();
        for ( $i = $#activeClients; $i >= 0; $i-- ) {
            $e = $activeClients[$i]->{id};
            if ( $e != NONE ) {
                $idlesecs = $main::now - $main::objects[$e]{"last"};
                $idle     = Purl::filter::timeFormat($idlesecs);
                $onsecs   = $main::now - $main::objects[$e]{"on"};
                $on       = Purl::filter::timeFormat($onsecs);
                my $name = sprintf( "%-15.15s", $main::objects[$e]{"name"} );
                push @lines,
                    sprintf( "%s%10.10s%7.7s%-47s",
                    $name, $on, " $idle ", $main::objects[$e]{"doing"} );
            }
        }
        $tData{lines} = \@lines;
    }
    $tData{footer} = "Uptime: "
        . &Purl::filter::timeFormat( $main::now - $main::initialized );
    my $text = processTemplate(%tData);
    print $text;
}

1;
